<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\IntegralGoods;
use App\Http\Requests\Admin\IntegralGoodsRequest;
use App\Model\IntegralRecord;
use App\Http\Controllers\Common\CommonController;
use App\Http\Controllers\Common\ArrayController;

/**
     * 积分商品表
     *
     * @author 吴国庆
     *
     * @return \Illuminate\Http\Response
     */

class IntegralController extends Controller
{
    /**
     * [index 提取积分商品页数据]
     * @return Admin/Integral/goods 加载积分商品页
     */
    public function index()
    {

        $state = [1=>'新上架', 2=>'在售', 3=>'已下架'];
    	$integral_goods = IntegralGoods::select('id', 'ig_name', 'image', 'detail','integral_price', 'status', 'stock', 'date_issued', 'date_under')->where('id','>', 0)->paginate(8);

    	//dd($integral_goods);
        return view('Admin/Integral/goods', ['goods' => $integral_goods, 'state' => $state]);
    }
    

    /**
     * [create 进入添加积分商品页]
     * @return Admin/Integral/create 加载添加积分商品页
     */
    public function create()
    {
        return view('Admin/Integral/create');
    }

    
    public function json($code, $msg, $data = [])
    {
        return response()->json([
            'code' => $code,
            'msg'    => $msg,
            'data'  => $data,
        ]);
    }

    /**
     * [store 处理添加积分商品数据]
     * @return [json] json格式返回状态码与信息
     */
    public function store(IntegralGoodsRequest $request)
    {
    	//dd($request);
    	$common = new CommonController;
        $name = $request->input('name');
        $detail = $request->input('detail');
        $price = $request->input('price');
        $stock = $request->input('stock');

        $resu = $common->uploadToQiNiu($request, 'image');
        if ($resu) {
            $images = $common->downloadFile($resu['key']);
        }else{ 
        	return redirect('admin/integral/create')->with('errorTip','图片不能为空');
        }
       	 
        $res = IntegralGoods::insertGetId([

        	'ig_name' => trim($name),
        	'image' => $images,
        	'detail' => $detail,
        	'integral_price' => $price,
        	'status' => '1',
        	'stock' => $stock,
        	'date_issued' => date('Y-m-d H:i:s',time())
        ]);
        //dd($res);
        if ($res) {
            $common->admin_log(session('adminUser')['username'], 10, "添加积分商品");
            return redirect('admin/integral/create')->with('msg','添加成功');
        } else {        
            return redirect('admin/integral/create')->with('msg','添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function shows($id)
    {
        //

    	$list = IntegralRecord::select('id', 'detail', 'change', 'date')->where('user_id', $id)->paginate(8);

        return view('Admin/Integral/integral', ['list' => $list]);
    }

    /**
     * [edit 加载原积分商品的数据]
     * @return  Admin/Integral/edit 进入积分商品修改页面
     */
    public function edit($id)
    {
        //
        $good = IntegralGoods::select('id', 'ig_name', 'image', 'detail','integral_price', 'stock', 'status')->where('id', $id)->get();
        $goods = $good->toArray();

        $state = $goods[0]['status'];
        //dd($state);
        return view('Admin/Integral/edit', ['goods' => $goods, 'state' => $state]);

    }

    /**
     * [update 处理修改的积分商品数据]
     * @return [json] json格式返回状态码与信息
     */
    public function update(IntegralGoodsRequest $request)
    {
    	//dd($request->pic);
        $common = new CommonController;
      	$id =  $request->input('id'); 
      	$name = $request->input('name');
        $detail = $request->input('detail');
        $price = $request->input('price');
        $stock = $request->input('stock');
        $status = $request->input('status');
       
        $resu = $common->uploadToQiNiu($request, 'image');
        if ($resu) { 
        	$images = $common->downloadFile($resu['key']);
        }else{ 
        	$images = $request->pic;
        }
	    

        $time = '';

		if ($status == '3') { 

			$state = IntegralGoods::select('status')->where('id', $id)->first();

			if ($state->status !== '3') { 

				$date_under = time();

				$time = date('Y-m-d H:i:s',$date_under);

			}
		}   

		
		//return IntegralGoods::select('status')->where('id', $id)->first();
		$res = IntegralGoods::where('id', $id)->update([
        	'ig_name' => trim($name),
        	'image' => $images,
        	'detail' => $detail,
        	'integral_price' => $price,
        	'stock' => $stock,
        	'status' => $status,
        	'date_under' => $time
        ]);
		//return $res;
        if ($res) {
            $common->admin_log(session('adminUser')['username'], 11, "修改积分商品");
            return redirect('admin/integral')->with('cont','修改成功');
        } else {
            return redirect('admin/integral')->with('cont','修改失败');
        }
    }

   

    /**
     * destroy 执行删除积分商品
     * @return 处理结果
     */
    public function destroy($id)
    {
        //
    	$bool = IntegralGoods::where('id', $id)->delete();

    	return redirect('admin/integral')->with('msg','删除成功');
    }

    
    
}
